<?php
require_once('./includes/headers.inc.php');

$error = FALSE;
$hash = (isset($_GET['hash']) ? $_GET['hash'] : false);

if($userinfo = $userMgr->validateResetPasswordHash($hash)) {
    if($_POST) {
        if($_POST['password'] && $_POST['password'] === $_POST['password_confirm']) {
            $ret = $userMgr->resetPassword($userinfo['id'], $_POST['password']);
            if($ret) {
                $_SESSION['msg'] = $msgMgr->get('password-change-success');
                $userMgr->login($userinfo['username'], $_POST['password']);
                header('Location: /');
                exit();
            } else {
                $error = $msgMgr->get('password-change-error');
            }
        } else {
            $error = $msgMgr->get('password-match-error');
        }
    }
} else {
    $error = $msgMgr->get('password-reset-link-error');
}

$replaces = array(
    'error'             => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_reset_password'     => $langMgr->get('header_reset_password'),
    'text_newpassword'          => $langMgr->get('text_newpassword'),
    'text_newpassword_confirm'  => $langMgr->get('text_newpassword_confirm'),
    'btn_submit'                => $langMgr->get('btn_submit'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
